/*
 * BanBu_PhotoViewController
 * @description 本文件提供对话中选择图片界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供对话中选择图片界面的ViewController
 */

#import "BanBu_PhotoViewController.h"
#import "BanBu_NavButton.h"
#import <MediaPlayer/MediaPlayer.h>
#import "AppCommunicationManager.h"
#import "UIViewController+Animation.h"
#import "BanBuAPIs.h"
#import "BanBu_ChatCellAPI.h"

@interface BanBu_PhotoViewController ()

@end

@implementation BanBu_PhotoViewController
@synthesize type_file = _type_file;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    [self setViewType:self.type];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setViewType:(MediaMsgType)type
{
    if (type == MediaMsgTypeImage)
    {
        double db = self.image.size.width/self.image.size.height;
        [UIApplication sharedApplication].statusBarHidden = YES;
        
        if (self.image.size.height == self.image.size.width || self.image.size.height < self.image.size.width || self.image.size.width>__MainScreen_Width)
        {
            self.imageView = [[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width/db)] autorelease];
            
            self.imageView.center = CGPointMake(self.view.center.x, self.view.center.y);
        }
        else
        {
            self.imageView = [[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
        }
        
        [self.view addSubview:self.imageView];
        
        [self setCustImage:self.image];
        
    }
    else
    {
        [UIApplication sharedApplication].statusBarHidden = YES;
        if(!IS_IOS_7)
        {
            self.wantsFullScreenLayout = YES;
        }
        if (self.moviePlayer == nil)
        {
            NSString * videoPath =[NSString stringWithFormat:@"%@.mp4",[AppComManager pathForVideoAlbumMedia:self.url]];
            self.moviePlayer = [[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:videoPath]] autorelease];
        }
        
        self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
        [self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 240)];
        self.moviePlayer.view.center = CGPointMake(self.view.center.x, self.view.center.y - 40);

        [self.moviePlayer prepareToPlay];
        [self.view addSubview:self.moviePlayer.view];
    }
    
    UIView * bottomBarView = [[UIView alloc]initWithFrame:CGRectMake(0, __MainScreen_Height-25, __MainScreen_Width, 45)];
    bottomBarView.backgroundColor = [UIColor blackColor];
    bottomBarView.alpha = 0.4;
    [self.view addSubview:bottomBarView];
    
    CGFloat btnLen = [NSLocalizedString(@"cancelNotice", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 30)].width;
    
    //取消按钮
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 10;
    [button setBackgroundColor:[UIColor darkGrayColor]];
    [button setFrame:CGRectMake(10, __MainScreen_Height-20, btnLen+20, 35)];
    [button setTitle:NSLocalizedString(@"cancelNotice", nil) forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button addTarget:self action:@selector(popself:) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.imageView.frame.size.height == 320)
    {
        [button setBackgroundColor:[UIColor darkGrayColor]];
    }
    
    [self.view addSubview:button];
    
    //选择按钮
    btnLen = [NSLocalizedString(@"sendReply", nil) sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 30)].width;
    
    UIButton * done = [UIButton buttonWithType:UIButtonTypeCustom];
    done.layer.cornerRadius = 10;
    [done setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"topbar.png"]]];
    [done setTintColor:[UIColor whiteColor]];
    done.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [done setFrame:CGRectMake(320-10-btnLen-20, __MainScreen_Height-20,btnLen+20, 35)];
    [done setTitle:NSLocalizedString(@"sendReply", nil) forState:UIControlStateNormal];
    [done addTarget:self action:@selector(selected:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:done];
}

-(void)setCustImage:(UIImage *)image
{
    self.imageView.image = image;
}

-(void)selected:(UIButton *)sender
{
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    NSDictionary * postDic = [NSDictionary dictionaryWithObjectsAndKeys:self.image,@"image",[NSNumber numberWithInt:ChatCellTypeImage],KeyType,@"mo",KeyShowFrom, nil];
    if (self.enablePost)
    {
        if (self.type == MediaMsgTypeImage)
        {
            [[NSNotificationCenter defaultCenter]postNotificationName:kSendMsgAllTypeNotification object:postDic];
        }
        else
        {
            [self.moviePlayer stop];
            if (_type_file == FILE_KOOLALBUM)
            {
                [[NSNotificationCenter defaultCenter]postNotificationName:@"videoAlbumPopToChat" object:nil];
                //返回照片展示界面
                NSDictionary * sendDic = [NSDictionary dictionaryWithObjectsAndKeys:self.url,@"video", nil];
                [[NSNotificationCenter defaultCenter]postNotificationName:kSendLocalVideoNotification object:sendDic];
                
            }
            else
            {
                //返回聊天界面
                NSDictionary * sendDic = [NSDictionary dictionaryWithObjectsAndKeys:self.url,@"video",[NSNumber numberWithInt:ChatCellTypeVideo],KeyType,@"mo",KeyShowFrom, nil];
                
                [[NSNotificationCenter defaultCenter]postNotificationName:@"videoAlbumPopToChat" object:sendDic];
                [[NSNotificationCenter defaultCenter]postNotificationName:kSendMsgAllTypeNotification object:sendDic];
            }

//            //返回聊天界面
//            NSDictionary * sendDic = [NSDictionary dictionaryWithObjectsAndKeys:self.url,@"video",[NSNumber numberWithInt:ChatCellTypeVideo],KeyType,@"mo",KeyShowFrom, nil];
//            
//            [[NSNotificationCenter defaultCenter]postNotificationName:@"videoAlbumPopToChat" object:sendDic];
//            [[NSNotificationCenter defaultCenter]postNotificationName:kSendMsgAllTypeNotification object:sendDic];
        }
    }
    else
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"setBGImage" object:postDic];
    }
    [self dismissViewControllerAnimated:YES];
}

-(void)popself:(UIButton *)sender;
{
    if (self.type == MediaMsgTypeImage)
    {
        [UIApplication sharedApplication].statusBarHidden = NO;
        [self.picker popViewControllerAnimated:YES];
    }
    else
    {
        [UIApplication sharedApplication].statusBarHidden = NO;
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
        
        //返回聊天界面
        [[NSNotificationCenter defaultCenter]postNotificationName:@"videoAlbumPopToChat" object:nil];
        [self dismissViewControllerAnimated:YES];
    }
}

-(void)dealloc
{
    [_url release],_url = nil;
    [_image release],_image = nil;
    [_imageView release],_imageView = nil;
    [_picker release],_picker = nil;
    [super dealloc];
}


@end
